﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace InSys.Documents
{

    public partial class FormSearchDocument : Form
    {
        private MapTestLinqDataContext db;
        private string registryNumber;
        private DateTime registryDate;
        private string content;
        private string enterprise;
        private int internalNumber;
        private string localID;
        private bool notUsed;
        private int userID;
        private string address;

        private bool blockEvent;

        public string RegistryNumber { get { return registryNumber; } set { registryNumber = value; regNumTextBox.Text = registryNumber; } }
        public DateTime RegistryDate { get { return registryDate; } set { registryDate = value; dateTimePicker1.Value = registryDate; } }
        public string Content { get { return content; } set { content = value; ContentTextBox.Text = content; } }
        public string Enterprise { get { return enterprise; } set { enterprise = value; EnterpriseTextBox.Text = enterprise; } }
        public int InternalNumber { get { return internalNumber; } set { internalNumber = value; InternalNumberTextBox.Text = internalNumber.ToString(); } }
        public string LocalID { get { return localID; } set { localID = value; LocalIDTextBox.Text = localID.ToString(); } }
        public bool NotUsed { get { return notUsed; } set { notUsed = value; if (notUsed == true) NotUsePersonCheckBox.CheckState = CheckState.Checked; else  NotUsePersonCheckBox.CheckState = CheckState.Unchecked; } }
        public int UserID { get { return userID; } 
            set 
            { 
                userID = value;
                PersonComboBox.SelectedValue = userID;
            } 
        }
        public string Address { get { return address; } set { address = value; } }

        public delegate void FormSearchDocumentEventHandler(object sender, EventArgs e);
        public event FormSearchDocumentEventHandler FormSearchEvent;


        private void FireFormSearchEvent()
        {
            if (FormSearchEvent != null)
            {
                EventArgs e = new EventArgs();
                FormSearchEvent(this, e);
            }
        }

        public FormSearchDocument()
        {
            blockEvent = false;
            InitializeComponent();
            clear();
        }

        private void clear()
        {
            RegistryNumber = "";
            RegistryDate = DateTime.Today;
            Content = "";
            Enterprise = "";
            InternalNumber = 0;
            LocalID = "";
            NotUsed = true;
            address = "";
        }

        private void buttonSearch_Click(object sender, EventArgs e)
        {
            registryNumber = regNumTextBox.Text;
            registryDate = dateTimePicker1.Value;
            content = ContentTextBox.Text;
            enterprise = EnterpriseTextBox.Text;
            try
            {
                internalNumber = Convert.ToInt32(InternalNumberTextBox.Text);
            }
            catch (Exception)
            {
                internalNumber = 0;
            }
            
            try
            {
                localID = LocalIDTextBox.Text.Trim();
            }
            catch (Exception)
            {
                localID = "";
            }
            notUsed = NotUsePersonCheckBox.CheckState == CheckState.Checked;
            address = AddressTextBox.Text;
            userID = (int)PersonComboBox.SelectedValue;
            FireFormSearchEvent();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            clear();
        }

        private void FormSearchDocument_Load(object sender, EventArgs e)
        {
            blockEvent = false;
            db = new MapTestLinqDataContext();
            PersonComboBox.DataSource = from item in db.persons orderby item.name select item;
            if (false == notUsed)
                PersonComboBox.SelectedValue = userID;
        }

        private void NotUsePersonCheckBox_CheckStateChanged(object sender, EventArgs e)
        {
            PersonComboBox.Enabled = !(NotUsePersonCheckBox.CheckState == CheckState.Checked);
            blockEvent = false;
        }
    }
}
